<?php

namespace app\mobile\controller;

use app\common\service\WxTokenService;
use library\Controller;
use think\Db;

/**
 * 应用入口
 * Class Index
 * @package app\mobile\controller
 */
class Index extends Base
{
    /**
     * 首页
     */
    public function index()
    {
    	if(isset($_GET['code'])){
			$code = $this->request->get('code');
			$wxtoken = new WxTokenService();
        	$openid = $wxtoken->getOpenidFromMp($code);
        	session('wechat_openid', $openid);
		}else{
			$wechat_openid = session('wechat_openid');
			if(empty($wechat_openid) || !$wechat_openid){
				session('wechat_openid', '');
			}			
		}
    	
    	$this->company_name = $this->company['name'];
		$this->point = $this->company['point'];	
		$contract = Db::name('system_contract')->where(['company_id'=>$this->company['id']])->order('id desc')->field('id,status,end_date,setmeal_type,people,account_type')->find();
//		if(!$contract){			
//			$this->fetch('noindex');    
//		}
		$this->contract = $contract;
		$this->end_date = $contract['end_date'];
		$this->status = $contract['status'];
		$this->people = $contract['people'];
		//公司类型
		$member = Db::name('system_member')->where(['id'=>$this->company['mid']])->find();
		$this->account_type = $member['account_type'];
		if($member['account_type'] == 2){
			$this->master_name = Db::name('system_company')->where(['id'=>$member['master']])->value('name');
		}
		//查看有没有待付款的社保账单
		$this->insbill = Db::name('system_insbill')->where(['company_id'=>$this->company['id'],'status'=>2,'is_deleted'=>0])->count();
		//查看有没有待付款的增值账单
		$this->cadditional_bill = Db::name('system_cadditional_bill')->where(['company_id'=>$this->company['id'],'status'=>2,'is_deleted'=>0])->count();
		//在职人数
		$this->in_bao = Db::name('system_company_person')->where(['company_id'=>$this->company['id'],'status'=>1])->count();		
		//客服信息
		$this->csr = Db::name('system_user')->where(['id'=>$this->company['csr_id']])->field('nickname,qq,phone')->find();
		if($contract['setmeal_type'] == 2 || !$contract){
			//判断是否提醒续约
			if($contract['end_date'] && $contract['status'] == 1){
				$today = date('Y-m-d',time());
		    	$month = after_month($today,1);
		        $lastday = get_lastday($month);
		        $end_date = $month.'-'.$lastday;
		        $end_date = strtotime($end_date);
		        if(strtotime($contract['end_date']) <= $end_date){
		        	$this->reminder = 1;
		        }
			}		 					
			$this->fetch('newindex');    
		}else{
			//判断是否提醒续约
			if($contract['account_type'] == 1 && $this->company['point'] <= 5 && $contract['status'] == 1){
				$this->reminder = 1;
			}
	    	$this->fetch();       
		}
		   
    }


	/**
     * 参保统计
     */
    public function Statistics()
    {
		$contract = Db::name('system_contract')->where(['company_id'=>$this->company['id']])->order('id desc')->field('id,status,end_date,setmeal_type,people')->find();
		if(!$contract){			
			//养老人数
			$this->ylnum =  0;
			//工伤人数
			$this->gsnum = 0;
			//失业人数
			$this->synum = 0;
			//医疗人数
			$this->yilnum = 0;
			//公积金人数
			$this->gjjnum = 0;
			//托管档案人数
			//$inbaoids = Db::name('system_company_person')->where(['company_id'=>$this->company['id'],'status'=>1])->column('person_id');
			$this->archives = 0;
		}else{
			//养老人数
			$this->ylnum = Db::name('system_insurance_log')->where(['company_id'=>$this->company['id'],'contract_id'=>$contract['id'],'status'=>1,'insurance'=>1])->count();
			//工伤人数
			$this->gsnum = Db::name('system_insurance_log')->where(['company_id'=>$this->company['id'],'contract_id'=>$contract['id'],'status'=>1,'insurance'=>2])->count();
			//失业人数
			$this->synum = Db::name('system_insurance_log')->where(['company_id'=>$this->company['id'],'contract_id'=>$contract['id'],'status'=>1,'insurance'=>3])->count();
			//医疗人数
			$this->yilnum = Db::name('system_insurance_log')->where(['company_id'=>$this->company['id'],'contract_id'=>$contract['id'],'status'=>1,'insurance'=>4])->count();
			//公积金人数
			$this->gjjnum = Db::name('system_insurance_log')->where(['company_id'=>$this->company['id'],'contract_id'=>$contract['id'],'status'=>1,'insurance'=>5])->count();
			//托管档案人数
			//$inbaoids = Db::name('system_company_person')->where(['company_id'=>$this->company['id'],'status'=>1])->column('person_id');
			$this->archives = Db::name('system_archives')->where(['company_id'=>$this->company['id'],'status'=>2,'person_status'=>1])->count();
		}
		
    	$this->fetch();   
    }

	
	/**
     * 业务
     */
    public function Business()
    {
    	//增员服务未通过
    	$this->increase = Db::name('system_increase')->where(['company_id'=>$this->company['id'],'is_deleted'=>0,'status'=>3])->count();
    	//减员服务未通过
    	$this->downsizing = Db::name('system_downsizing')->where(['company_id'=>$this->company['id'],'is_deleted'=>0,'status'=>3])->count();
    	//增员确认未通过
    	$this->incconfig = Db::name('system_increase_insurance')->where(['company_id'=>$this->company['id'],'is_deleted'=>0,'status'=>4])->count();
    	//减员确认未通过
    	$this->downconfig = Db::name('system_downsizing_insurance')->where(['company_id'=>$this->company['id'],'is_deleted'=>0,'status'=>3])->count();
    	$this->fetch(); 
    }
    
    
    /**
     * 套餐详情
     */
    public function package()
    {
    	$type = $this->company['setmeal_type'];
    	$list = Db::name('system_package')->where(['is_deleted'=>0,'type'=>$type,'status'=>1,'show'=>1])->select();
    	foreach($list as $k=>$v){
    		$yu = $v['month'] % 12;
    		if($yu == 0){
    			$list[$k]['service_time'] = ($v['month'] / 12).'年';
    		}else{
    			$list[$k]['service_time'] = $v['month'].'个月';
    		}
    	}
    	$this->list = $list;
    	$this->type = $type;
    	$this->fetch();
    }
    
    
    /**
     * 套餐详细信息
     */
    public function details()
    {
    	if($this->request->isPost()){
    		$data = $this->request->post();
    		if(empty($data['id']) || !$data['id']){
				$this->error('参数错误');
			}
			$package = Db::name('system_package')->where(['is_deleted'=>0,'show'=>1,'id'=>$data['id']])->find();
			if(!$package){
	    		$this->error('套餐错误，无法下单');
	    	}
	    	//生成订单
	    	$arr = [
	    		'code' => $this->company['unique'].'3'.mt_rand (10000,99999),
	    		'company_id' => $this->company['id'],
	    		'package_id' => $data['id'],
	    		'setmeal_type' => $package['type'],
	    		'type' => 1,
	    		'status' => 1,
	    		'receivable' => $package['price'],
	    		'balance' => (float)sprintf("%.2f",0 - $package['price'])
	    	];
	    	$re = Db::name('system_package_order')->insertGetId($arr);
	    	if($re){
	    		$ins = [
	        		'code' => $arr['code'],
	        		'jump_id' => $re,
	        		'company_id' => $this->company['id'],
	        		'bill_type' => 2,
	        		'cost_type' => 2,
	        		'receivable' => $arr['receivable'],
	        		'balance' => $arr['balance']
	        	];
	        	Db::name('system_financial_bill')->insert($ins);
	        	$this->success('订单创建成功', ['id'=>$re]);
	    	}
    	}else{
    		$id = $this->request->get('id');
	    	$package = Db::name('system_package')->where(['is_deleted'=>0,'show'=>1,'id'=>$id])->find();
	    	if(!$package){
	    		$this->redirect('mobile/publicot/transition', ['info'=>'暂无该套餐','type'=>'7']);
	    	}
	    	$yu = $package['month'] % 12;
    		if($yu == 0){
    			$package['service_time'] = ($package['month'] / 12).'年';
    		}else{
    			$package['service_time'] = $package['month'].'个月';
    		}
	    	$this->package = $package;
	    	$this->fetch();
    	}
    	
    }
    
    
    /**
     * 判断能否续约
     */
    private function is_renewal()
    {
    	$contract = Db::name('system_contract')->where(['company_id'=>$this->company['id'],'status'=>1])->find();
    	if(!$contract){
    		return false;
    	}
    	if($contract['setmeal_type'] == 1 && $contract['account_type'] == 2){
    		return false;
    	}
    	return true;
//  	$contract = Db::name('system_contract')->where(['company_id'=>$this->company['id'],'setmeal_type'=>[2,3],'status'=>1])->find();
//  	if(!$contract){
//  		return false;
//  	}
//  	$after_month = after_month($contract['end_date'],1);
//  	$before_month = before_month($contract['end_date'],1);
//  	$start_time = strtotime($before_month);
//  	$end_time = strtotime($after_month);
//  	if((time() < $start_time) || (time() >= $end_time)){
//  		return false;
//  	}else{
//  		return true;
//  	}  
    }
    
    /**
     * 判断能否续约
     */
    public function renewal_check()
    {  
    	$re = $this->is_renewal();
    	if(!$re){
    		$this->error('无法续约');
    	}else{
    		$this->success('可以续约');
    	}
    }
    
    
    
    /**
     * 续约套餐
     */
    public function renewal_package()
    {
    	$contract = Db::name('system_contract')->where(['company_id'=>$this->company['id'],'status'=>1])->order('id desc')->find();
    	if(!$contract){
    		$this->redirect('index/publicot/transition', ['info'=>'暂时无法续约','type'=>'9']);
    	}
    	if($contract['setmeal_type'] == 1 && $contract['account_type'] == 2){
    		$this->redirect('index/publicot/transition', ['info'=>'暂时无法续约','type'=>'9']);
    	}  
    	$list = Db::name('system_package')->where(['is_deleted'=>0,'type'=>$contract['setmeal_type'],'status'=>1,'show'=>1])->select();
    	foreach($list as $k=>$v){
    		$yu = $v['month'] % 12;
    		if($yu == 0){
    			$list[$k]['service_time'] = ($v['month'] / 12).'年';
    		}else{
    			$list[$k]['service_time'] = $v['month'].'个月';
    		}
    	}
    	$this->list = $list;
    	$this->type = $contract['setmeal_type'];
    	$this->fetch();
    }
    
    
    
    /**
     * 续约套餐详细信息
     */
    public function renewal_details()
    {
    	if($this->request->isPost()){
    		$data = $this->request->post();
    		if(empty($data['id']) || !$data['id']){
				$this->error('参数错误');
			}
			$res = $this->is_renewal();
	    	if(!$res){
	    		$this->error('无法续约');
	    	}
			$package = Db::name('system_package')->where(['is_deleted'=>0,'show'=>1,'id'=>$data['id']])->find();
			if(!$package){
	    		$this->error('套餐错误，无法下单');
	    	}
	    	//生成订单
	    	$arr = [
	    		'code' => $this->company['unique'].'4'.mt_rand (10000,99999),
	    		'company_id' => $this->company['id'],
	    		'package_id' => $data['id'],
	    		'setmeal_type' => $package['type'],
	    		'type' => 2,
	    		'status' => 1,
	    		'receivable' => $package['price'],
	    		'balance' => (float)sprintf("%.2f",0 - $package['price'])
	    	];
	    	$re = Db::name('system_package_order')->insertGetId($arr);
	    	if($re){
	    		$ins = [
	        		'code' => $arr['code'],
	        		'jump_id' => $re,
	        		'company_id' => $this->company['id'],
	        		'bill_type' => 2,
	        		'cost_type' => 2,
	        		'receivable' => $arr['receivable'],
	        		'balance' => $arr['balance']
	        	];
	        	Db::name('system_financial_bill')->insert($ins);
	        	$this->success('订单创建成功', ['id'=>$re]);
	    	}
    	}else{
    		$re = $this->is_renewal();
	    	if(!$re){
	    		$this->redirect('mobile/publicot/transition', ['info'=>'暂时无法续约','type'=>'9']);
	    	}
    		$id = $this->request->get('id');
	    	$package = Db::name('system_package')->where(['is_deleted'=>0,'show'=>1,'id'=>$id])->find();
	    	if(!$package){
	    		$this->redirect('mobile/publicot/transition', ['info'=>'暂无该套餐','type'=>'7']);
	    	}
	    	$yu = $package['month'] % 12;
    		if($yu == 0){
    			$package['service_time'] = ($package['month'] / 12).'年';
    		}else{
    			$package['service_time'] = $package['month'].'个月';
    		}
	    	$this->package = $package;
	    	$this->fetch();
    	}
    	
    }
    
}
